
package pcmd_gui;

use strict;
use warnings;

use Tk;
use Tk::Adjuster;
use Tk::widgets qw/Trace/;
use Tk::ItemStyle;
use Tk::DialogBox;
use Tk::Dialog;
use Tk::ErrorDialog;


use pcmd_tab;

sub new {
    my $arg0 = $_[0];
    my $cls = ref($arg0) || $arg0;
    
    my $self = {};
    bless $self, $cls;

    $self->init($_[1], $_[2], $_[3]);
    
    return $self;
}


#--------------
# Init class --
#--------------
sub init {
    my $self = shift;
    my $pcmd = shift;
    my $config = shift;
    my $texts = shift;
    

    # Store params
    $self->{pcmd} = $pcmd;
    $self->{config} = $config;
    $self->{texts} = $texts;


    # Create main window
    my $mw = MainWindow->new(-name => "PerlCommander", -width => 800, -height => 600,);
    $mw->minsize( 800, 600);
    $self->{'mw'} = $mw;

    # Create menubar
    $self->create_menubar();

    # Create tabs
    $self->create_tabs();

    # Create buttons
    my $bottom_frame = $self->{'mw'}->Frame->pack(-side => 'bottom', -fill => 'x', );
    $self->{'bottom'}->{'frame'} = $bottom_frame;
    
    $bottom_frame->Button(-text => 'F3 View', -command => sub { $self->{pcmd}->callback_F3();}, )->pack(-side => 'left', -fill => 'both', -expand => '1');
    $bottom_frame->Button(-text => 'F4 Edit', -command => sub { $self->{pcmd}->callback_F4();}, )->pack(-side => 'left', -fill => 'both', -expand => '1');
    $bottom_frame->Button(-text => 'F5 Copy', -command => sub { $self->{pcmd}->callback_F5();}, )->pack(-side => 'left', -fill => 'both', -expand => '1');
    $bottom_frame->Button(-text => 'F6 Move', -command => sub {print "F4\n"}, )->pack(-side => 'left', -fill => 'both', -expand => '1');
    $bottom_frame->Button(-text => 'F7 New folder', -command => sub { $self->{pcmd}->callback_F7(); }, )->pack(-side => 'left', -fill => 'both', -expand => '1');
    $bottom_frame->Button(-text => 'F8 Delete', -command => sub { $self->{pcmd}->callback_F8(); }, )->pack(-side => 'left', -fill => 'both', -expand => '1');
    
    # Bindings
    $self->set_bindings();

    # Get images
    $self->get_images();
    
    # Styles
    $self->init_styles();
    
}


#-------
# Run --
#-------
sub run {
    my $self = shift;

    MainLoop;
};


#---------------
# Create tabs --
#---------------
sub create_tabs {
    my $self = shift;    
    
    # Top frame
    my $top_frame = $self->{'mw'}->Frame->pack(-side => 'top', -fill => 'both', -expand => '1');
    
    # Left tab    
    my $left_frame = $top_frame->Frame->pack(-side => 'left', -fill => 'both', -expand => '1');    
    my $left_callbacks = {
        'focus_in'    => sub { $self->{current_tab} = 'left'; },
        'command'     => sub { $self->{pcmd}->callback_command('left'); }, 
        'title_mod'   => sub { $self->{pcmd}->change_dir('left', $_[0]); },    
        'tab'         => sub { $self->{'right'}->focus(); Tk->break();},
    };
    
    $self->{left} = pcmd_tab->new($self, $self->{config}, $left_frame, 'left', $left_callbacks, );
    
    # Adjuster
    #my $adjuster = $self->{'mw'}->Adjuster(-widget => $left_frame, -side => 'left')->pack(-side => 'left', -fill => 'y');
    #$self->{'adjuster'} = $adjuster;

    # Right tab
    my $right_frame = $top_frame->Frame->pack(-side => 'right', -fill => 'both', -expand => '1');
    my $right_callbacks = {
        'focus_in'    => sub { $self->{current_tab} = 'right'; },
        'command'     => sub { $self->{pcmd}->callback_command('right'); }, 
        'title_mod'   => sub { $self->{pcmd}->callback_title('right', $_[0]); },            
        'tab'         => sub { $self->{'left'}->focus();  Tk->break(); },
    };
    
    $self->{right} = pcmd_tab->new($self, $self->{config}, $right_frame, 'right', $right_callbacks);


    # Set current tab
    $self->{current_tab} = 'left';
    
    # Trace to current_tab
    $self->{mw}->traceVariable(\$self->{current_tab}, 'w' => sub {
            my $other = ($_[1] eq 'right') ? 'left' : 'right';
            $self->{$other}->clear_anchor();
            $self->{$_[1]}->set_anchor(1);
                    
            return $_[1];
        }
    );
    
    # Set focus to left
    $self->{left}->focus();

};


#-------------------
# Create menu bar --
#-------------------
sub create_menubar {
    my $self = shift;
    
    # Create menu bar
    my $menubar = $self->{'mw'}->Menu();
    $self->{'menu'}->{'bar'} = $menubar;
    $self->{'mw'}->configure(-menu=> $menubar);


    # Create submenus
    my $file = $menubar->cascade(-label => 'File', -tearoff => 0);
    my $edit = $menubar->cascade(-label => 'Edit', -tearoff => 0);
    my $help = $menubar->cascade(-label => 'Help', -tearoff => 0); 
   
};


#----------------
# Set bindings --
#----------------
sub set_bindings {
    my $self = shift;
        
    # F*
    print "her\n";
    $self->{mw}->bind('<Key-F3>' => sub { $self->{pcmd}->callback_F3();}, );
    $self->{mw}->bind('<Key-F4>' => sub { $self->{pcmd}->callback_F4();}, );
    $self->{mw}->bind('<Key-F5>' => sub { $self->{pcmd}->callback_F5();}, );
    $self->{mw}->bind('<Key-F6>' => sub { $self->{pcmd}->callback_F6();}, );
    $self->{mw}->bind('<Key-F7>' => sub { $self->{pcmd}->callback_F7();}, );
    $self->{mw}->bind('<Key-F8>' => sub { $self->{pcmd}->callback_F8();}, );

};



#--------------
# Get images --
#--------------
sub get_images {
    my $self = shift;

    $self->{images}{dir} = $self->{mw}->Getimage('folder');
    $self->{images}{file} = $self->{mw}->Getimage('file');
};


#---------------
# Init styles --
#---------------
sub init_styles {
    my $self = shift;
    
    my $font = $self->{mw}->fontCreate(-slant => 'roman',);
    
    $self->{styles}{normal} = $self->{mw}->ItemStyle(
        'imagetext',
        #-font => $font,
        -background => $self->{config}{colors}{background},
        -foreground => $self->{config}{colors}{item_normal},
        -selectforeground => $self->{config}{colors}{item_normal},
    );
    
    $self->{styles}{selected} = $self->{mw}->ItemStyle(
        'imagetext',
        #-font => $font,
        -background => $self->{config}{colors}{background},
        -foreground => $self->{config}{colors}{item_selected},
        -selectforeground => $self->{config}{colors}{item_selected},
    );
    
};


#---------------
# Rebuild tab --
#---------------
sub rebuild_tab {
    my $self = shift;
    my $tab = shift;
    my $current_dir = shift;    
    
    $self->{$tab}->clear_selection();
    $self->{$tab}->set_title($current_dir);
    $self->{$tab}->update_tree(@_);
    if ($self->{current_tab} eq $tab) {
        $self->{$tab}->set_anchor();
    };    
};


#---------------
# Reload tab --
#---------------
sub update_tab {
    my $self = shift;
    my $tab = shift;
    
    $self->{$tab}->update_tree(@_);
    if ($self->{current_tab} eq $tab) {
        $self->{$tab}->set_anchor(1);
    };
};


#----------------
# Get selected --
#----------------
sub get_selected {
    my $self = shift;
    my $tab = shift;
    
    return $self->{$tab}->get_selected();
};


#-----------------
# Get selection --
#-----------------
sub get_selection {
    my $self = shift;
    my $tab = shift;
    
    return $self->{$tab}->get_selection();
};


#-----------
# Get tab --
#-----------
sub get_tab {
    my $self = shift;
    my $tab = shift;
    
    return $self->{current_tab};
};


#------------------------
# Dialog create folder --
#------------------------
sub dialog_create_folder {
    my $self = shift;
    
    my $dialog = $self->{mw}->DialogBox(
        -title => $self->{texts}{new_folder},
        -buttons => ['Ok', 'Cancel'], 
        -default_button => 'Ok',
    );
    
    my $folder_name = 'New_folder';
    my $labentry = $dialog->add('LabEntry', 
        -textvariable => \$folder_name, 
        -width => 20, 
        -label => $self->{texts}{name}, 
        -labelPack => [-side => 'left'],
        -background => $self->{config}{colors}{background},   
    )->pack;
    
    # Select whole text
    $labentry->selectionRange(0, 'end');
    
    # Set focus to labentry
    $dialog->configure(-focus => $labentry);
    
    my $answer = $dialog->Show( );
    
    if ($answer eq "Ok") {
        return $folder_name;
    } else {
        return undef;
    };
};


#-----------------
# Dialog remove --
#-----------------
sub dialog_remove {
    my $self = shift;
    my $files = shift;
    my $rmtree = shift;
    
    my $dialog = $self->{mw}->DialogBox(
        -title => $self->{texts}{t_remove_files},
        -buttons => ['Ok', 'Cancel'], 
        -default_button => 'Cancel',
    );
    
    my $folder_name = 'New_folder';
    $dialog->add('Label', 
        -text => sprintf($self->{texts}{l_remove_files}, 1),
    )->pack(-side => 'top');
    
    $dialog->add('Checkbutton', 
        -text => $self->{texts}{c_remove_files},
        -variable => $rmtree,        
    )->pack(-side => 'bottom');
    
    my $answer = $dialog->Show( );
    
    if ((defined $answer) and ($answer eq "Ok")) {
        return 1;
    } else {
        return undef;
    };
};


#-----------------------
# Dialog confirm copy --
#-----------------------
sub dialog_confirm_copy {
    my $self = shift;
    my $file = shift;
    
    my $dialog = $self->{mw}->DialogBox(
        -title => $self->{texts}{t_remove_files},
        -buttons => ['Overwrite', 'Skip', 'Overwrite all', 'Skip all'], 
        -default_button => 'Skip',
    );
    
    my $folder_name = 'New_folder';
    $dialog->add('Label', 
        -text => sprintf($self->{texts}{l_confirm_copy}, $file),
    )->pack(-side => 'top');
    
    my $answer = $dialog->Show( );
    
    return $answer;
};


#-----------------------
# Dialog error simple --
#-----------------------
sub dialog_error_simple {
    my $self = shift;
    my $msg = shift;
    
    $self->{mw}->Dialog(-title => $self->{texts}{error}, 
        -text => $msg, 
        -default_button => $self->{texts}{ok}, 
        -buttons => [ $self->{texts}{ok}], 
        -bitmap => 'error' )
    ->Show( );
    
};

1;

